﻿Public Class SetTime

    Public Enum FocusedItem
        None
        Hours
        Minutes
        Seconds
    End Enum

    Dim mFocusItem As FocusedItem
    Dim mHours As Integer = 0
    Dim mMinutes As Integer = 0
    Dim mSeconds As Integer = 0
    Dim mWidth As Integer
    Dim mHeight As Integer
    Dim mFont As Font
    Dim mBrush As Brush
    Dim mGrayBrush As Brush
    Dim mFocusBrush As SolidBrush


    Public Shadows Property Enabled() As Boolean
        Get
            Return MyBase.Enabled
        End Get
        Set(ByVal value As Boolean)
            MyBase.Enabled = value
            If value Then
                SetPanel.Image = Backgrounds.Images(2)
            Else
                mFocusItem = FocusedItem.None
                SetPanel.Image = Backgrounds.Images(1)
            End If
        End Set
    End Property


    Private Sub SetPanel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SetPanel.Click
        If Not MyBase.Enabled Then Exit Sub
        If mWidth = 240 Then
            Select Case MousePosition.X
                Case 14 To 50           ' Hours
                    FocusItem = FocusedItem.Hours
                Case 63 To 99           ' minutes
                    FocusItem = FocusedItem.Minutes
                Case 111 To 147         ' Seconds
                    FocusItem = FocusedItem.Seconds

                Case 150 To 188             ' Up
                    ChangeValue(1)
                Case Is > 188              ' Down
                    ChangeValue(-1)
            End Select
        End If
        If mWidth = 480 Then
            Select Case MousePosition.X
                Case 32 To 104           ' Hours
                    FocusItem = FocusedItem.Hours
                Case 126 To 198           ' minutes
                    FocusItem = FocusedItem.Minutes
                Case 222 To 294         ' Seconds
                    FocusItem = FocusedItem.Seconds

                Case 305 To 380             ' Up
                    ChangeValue(1)
                Case Is > 380              ' Down
                    ChangeValue(-1)
            End Select
        End If
    End Sub

    Private Sub SetPanel_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles SetPanel.DoubleClick
        SetPanel_Click(sender, e)
    End Sub
    Private Sub ChangeValue(ByVal Amount As Integer)
        If Not MyBase.Enabled Then Exit Sub
        Select Case mFocusItem
            Case FocusedItem.Hours
                mHours = (mHours + Amount) Mod 60
                If mHours < 0 Then mHours = 60 + mHours
            Case FocusedItem.Minutes
                mMinutes = (mMinutes + Amount) Mod 60
                If mMinutes < 0 Then mMinutes = 60 + mMinutes
            Case FocusedItem.Seconds
                mSeconds = (mSeconds + Amount) Mod 60
                If mSeconds < 0 Then mSeconds = 60 + mSeconds
        End Select
        SetPanel.Invalidate()
    End Sub
#Region "Properties"
    Public Property Hours() As Integer
        Get
            Return mHours
        End Get
        Set(ByVal value As Integer)
            mHours = value
            SetPanel.Invalidate()
        End Set
    End Property

    Public Property Minutes() As Integer
        Get
            Return mMinutes
        End Get
        Set(ByVal value As Integer)
            mMinutes = value
            SetPanel.Invalidate()
        End Set
    End Property

    Public Property Seconds() As Integer
        Get
            Return mSeconds
        End Get
        Set(ByVal value As Integer)
            mSeconds = value
            SetPanel.Invalidate()
        End Set
    End Property

    Public Property FocusItem() As FocusedItem
        Get
            Return mFocusItem
        End Get
        Set(ByVal value As FocusedItem)
            mFocusItem = value
            SetPanel.Invalidate()
        End Set
    End Property
#End Region
#Region "Slider Controls"
    Private mSliding As Boolean
    Private mStartY As Integer
    Private mInitialValue As Integer

    Private Sub SetPanel_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SetPanel.MouseDown
        If Not MyBase.Enabled Then Exit Sub
        If e.Button = Windows.Forms.MouseButtons.Left Then
            mSliding = True
            mStartY = e.Y
            Select Case MousePosition.X
                Case 14 To 50           ' Hours
                    FocusItem = FocusedItem.Hours
                    mInitialValue = mHours
                Case 63 To 99           ' minutes
                    FocusItem = FocusedItem.Minutes
                    mInitialValue = mMinutes
                Case 111 To 147         ' Seconds
                    FocusItem = FocusedItem.Seconds
                    mInitialValue = mSeconds
            End Select
        End If
    End Sub

    Private Sub SetPanel_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SetPanel.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Left Then
            mSliding = False
        End If
    End Sub

    Private Sub SetPanel_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SetPanel.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Select Case mFocusItem
                Case FocusedItem.Hours
                    mHours = (mInitialValue + (mStartY - e.Y) / 10) Mod 60
                    If mHours < 0 Then mHours = 60 + mHours
                Case FocusedItem.Minutes
                    mMinutes = (mInitialValue + (mStartY - e.Y) / 10) Mod 60
                    If mMinutes < 0 Then mMinutes = 60 + mMinutes
                Case FocusedItem.Seconds
                    mSeconds = (mInitialValue + (mStartY - e.Y) / 10) Mod 60
                    If mSeconds < 0 Then mSeconds = 60 + mSeconds
            End Select
            SetPanel.Invalidate()
        End If
    End Sub
#End Region
    Private Sub SetPanel_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles SetPanel.Paint
        Dim B As Brush

        If MyBase.Enabled Then
            B = mBrush
        Else
            B = mGrayBrush
        End If

        With e.Graphics
            If mWidth = 240 Then
                Select Case mFocusItem
                    Case FocusedItem.Hours
                        .FillRectangle(mFocusBrush, 16, 18, 36, 31)
                    Case FocusedItem.Minutes
                        .FillRectangle(mFocusBrush, 63, 18, 36, 31)
                    Case FocusedItem.Seconds
                        .FillRectangle(mFocusBrush, 111, 18, 36, 31)
                End Select

                .DrawString(mHours.ToString("00"), mFont, B, 20, 22)
                .DrawString(mMinutes.ToString("00"), mFont, B, 67, 22)
                .DrawString(mSeconds.ToString("00"), mFont, B, 116, 22)
            End If
            If mWidth = 480 Then
                Select Case mFocusItem
                    Case FocusedItem.Hours
                        .FillRectangle(mFocusBrush, 32, 37, 72, 62)
                    Case FocusedItem.Minutes
                        .FillRectangle(mFocusBrush, 126, 37, 72, 62)
                    Case FocusedItem.Seconds
                        .FillRectangle(mFocusBrush, 222, 37, 72, 62)
                End Select

                .DrawString(mHours.ToString("00"), mFont, B, 41, 40)
                .DrawString(mMinutes.ToString("00"), mFont, B, 135, 40)
                .DrawString(mSeconds.ToString("00"), mFont, B, 231, 40)
            End If
        End With
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        mFont = New Font("Tahoma", 16, FontStyle.Bold)
        mFocusBrush = New SolidBrush(Color.White)
        mBrush = New SolidBrush(Color.Black)
        mGrayBrush = New SolidBrush(Color.Gray)
        mWidth = Screen.PrimaryScreen.WorkingArea.Width
    End Sub
End Class
